﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;

namespace PATBController
{
    public static class Logger
    {
        private static string gsLogFile = null;
        private static string gsLogPath = null;
        private static string gsLogDateStamp = null;
        public static void Init(string sLogPath)
        {
            gsLogPath = sLogPath;
            gsLogDateStamp = DateTime.Now.ToString("yyyyMMdd");
            gsLogFile = gsLogPath + "\\" + Process.GetCurrentProcess().ProcessName + "_" + gsLogDateStamp + ".log";

            DirectoryInfo dirLogPath = new DirectoryInfo(gsLogPath);

            // clear any existing logs
            foreach (FileInfo f in dirLogPath.GetFiles("*.log"))
            {
                try { f.Delete(); }
                catch { }
            }

        }
        public static string GetLogFilename()
        {
            if(gsLogDateStamp != DateTime.Now.ToString("yyyyMMdd"))
            {
                try { File.Delete(gsLogFile); }
                catch { }
                gsLogDateStamp = DateTime.Now.ToString("yyyyMMdd");
                gsLogFile = gsLogPath + "\\" + Process.GetCurrentProcess().ProcessName + "_" + gsLogDateStamp + ".log";
            }
            return gsLogFile;
        }
        public static void Log(int iIndent, string sProcess, string sMessage)
        {
            Post(sProcess, sMessage, iIndent, true);
        }
        public static void Post(string sProcess, string sMessage)
        {
            Post(sProcess, sMessage, 0, false);
        }
        public static void Post(string sProcess, string sMessage, int iIndent, bool bLog)
        {

            string sSpaces = "";
            if( iIndent > 0 )
                sSpaces = new string(' ', iIndent * 2);
            string sPost = "[NSATBService(" + sProcess + ")] " + sSpaces + sMessage;
            Debug.WriteLine("General", sPost);
            Trace.AutoFlush = true;
            Trace.WriteLine("General", sPost);
            Console.WriteLine(DateTime.Now.ToString("yyyyMMdd-hhmmss: ") + sSpaces + sMessage);

            if(bLog && !string.IsNullOrEmpty(gsLogFile))
            {
                File.AppendAllText(GetLogFilename(), DateTime.Now.ToString("yyyyMMdd-hhmmss: ") + sSpaces + sMessage + Environment.NewLine);
            }
        }
        public static void PostException(Exception ex)
        {
            Debug.WriteLine("General", ex.ToString());
            Trace.AutoFlush = true;
            Trace.WriteLine("General", ex.ToString());
            Console.WriteLine(DateTime.Now.ToString("yyyyMMdd-hhmmss: ") + ex.ToString());

            if (!string.IsNullOrEmpty(gsLogFile))
            {
                File.AppendAllText(GetLogFilename(), DateTime.Now.ToString("yyyyMMdd-hhmmss: ") + ex.ToString());
            }
        }
    }
}
